//
//  RefreshView.swift
//  weibo1
//
//  Created by 李进 on 15/10/23.
//  Copyright © 2015年 李进. All rights reserved.
//

import UIKit

//MARK: 刷新控制器
class RefreshViewCV: UIRefreshControl {
    
    override init(){
        super.init()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }

    
    //懒加载控件x
    private lazy var refreshCView = RefreshView.resreshView()
    
    //var isStarts: Bool = false
    
    //重写 kvo的监听方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        if frame.origin.y > 0 {
            return
        }
        
        //print(self.frame)
        let ymin = CGFloat(-60)
        
        //判断是否正在刷新
        if refreshing {
            
            refreshCView.startsRefreshData()
            
            return
        }
        
        if frame.origin.y < ymin && !refreshCView.isStarts {
            
            self.refreshCView.isStarts = true
            
        }else if frame.origin.y >= ymin && refreshCView.isStarts {
            
            //refreshCView.stopAnima()
            
            refreshCView.isStarts = false
            
            
        }
        
    }
    
    
    //重写刷新方法
    override func beginRefreshing() {
        super.beginRefreshing()
        refreshCView.startsRefreshData()
    }
    //重写结束刷新方法
    override func endRefreshing() {
        super.endRefreshing()
        refreshCView.stopAnima()
    }

}

//MARK: 设置UI
extension RefreshViewCV {
    //设置UI控件
    func setupUI(){
        
        //隐藏一通显示的转轮
        tintColor = UIColor.clearColor()
        
        //添加控件
        addSubview(refreshCView)
        
        //设置刷新控件的自动布局
        refreshCView.snp_makeConstraints { (make) -> Void in
            //make.top.equalTo(self.snp_top)
            make.center.equalTo(self.snp_center)
            make.size.equalTo(refreshCView.bounds.size)
        }
        
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.addObserver(self, forKeyPath: "frame", options: [], context: nil)
        }
        
    }
}

//MARK: 在刷新控制器上加的view
class RefreshView:UIView{
    
    @IBOutlet weak var loadView: RefreshView!
    @IBOutlet weak var pullRefreshView: UIImageView!
    
    @IBOutlet weak var refreshImage: UIImageView!
    
    class func resreshView() -> RefreshView{
        
        let nib = UINib(nibName: "RefreshView", bundle: nil)
        
        return nib.instantiateWithOwner(nil, options: nil)[0] as! RefreshView
    }
    
    var isStarts: Bool = false {
        didSet{
            transFrameArc()
        }
    }
    
    //刷新指向图片的旋转
    func transFrameArc(){
        UIView.animateWithDuration(0.5) { () -> Void in
            self.pullRefreshView.transform = CGAffineTransformRotate(self.pullRefreshView.transform, CGFloat(M_PI))
        }
    }
    //正在刷新图片的动画
    func startsRefreshData(){
        
        loadView.hidden = false
        
        //使用关键帧动画来做
        let key = "transform.rotation"
        
        let basic = CABasicAnimation(keyPath: key)
        
        //basic.fromValue = 0
        
        basic.toValue = 2 * M_PI
        
        basic.repeatCount = MAXFLOAT
        
        basic.duration = 0.5
        
        basic.removedOnCompletion = false
        
        //添加动画
        refreshImage.layer.addAnimation(basic, forKey: key)
    }
    
    //停止动画
    func stopAnima(){
        
        loadView.hidden = true
        
        refreshImage.layer.removeAllAnimations()
        
    }
}

